﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace WP7SampleProject12
{

    public class MovieReview
    {
        public String User { set; get; }
        public String Comment { set; get; }
    }

    public partial class reviews : PhoneApplicationPage
    {
        private String movieText = "";
        public reviews()
        {
            InitializeComponent();
            PageTitle.Text = GlobalVariables.movieName;
            DownloadReviews();
        }
        
        


        private void DownloadReviews()
        {
            String URL = "http://facemeifudare.appspot.com/review?movie=" + GlobalVariables.movieName;

            progrssBar.IsIndeterminate = true;
            WebClient twitter = new WebClient();
            twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
            twitter.DownloadStringAsync(new Uri(URL));
             
           
        }


        void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            movieText = e.Result;
            String[] Reviews = movieText.Split('\n');
            if (Reviews.Length > 1)
                listBox1.ItemsSource = from item in Reviews select new MovieReview { User = item.Split(':')[0], Comment = item.Split(':')[1] };
            else
            {
                MessageBox.Show("No reviews found");
                NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
            }
            progrssBar.IsIndeterminate = false;
        }
    }

}